
*set working directory
use "anes_timeseries_2020_stata_20210719.dta", clear // load 2020 ANES
drop if V160001_orig == -1 // drop 2020 non-panelists
merge 1:1 V160001_orig using "anes_timeseries_2016.dta", keep(match) // merge in 2016

***** Sampling Variables ******
gen panel = 2016

gen caseid = .
replace caseid = V200001
label variable caseid "2020 Case ID"

gen postelection_t2 = .
replace postelection_t2 = 1 if V200004 == 3
replace postelection_t2 = 0 if V200004 == 1
drop if postelection_t2 != 1 // drop if did not complete 2020 post-election wave

gen weight = V200011b
gen strata = V200011d
gen psu = V200011c
label variable weight "Post-Stratification Weight"
label variable strata "Stratum"
label variable psu "PSU"

svyset[pweight=weight], strata(strata) psu(psu)

***** Party ID *****
gen pid16 = V161158x - 4 if V161158x > 0 
gen pid20 = V201231x - 4 if V201231x > 0 

gen rep16 = 1 if pid16 > 1
replace rep16 = 0 if pid16 < -1
gen rep20 = 1 if pid20 > 1
replace rep20 = 0 if pid20 < -1

gen pidstrength16 = 3 if abs(pid16) == 3
replace pidstrength16 = 2 if abs(pid16) == 2
replace pidstrength16 = 1 if abs(pid16) == 1
replace pidstrength16 = 0 if abs(pid16) == 0

gen pidstrength20 = 3 if abs(pid20) == 3
replace pidstrength20 = 2 if abs(pid20) == 2
replace pidstrength20 = 1 if abs(pid20) == 1
replace pidstrength20 = 0 if abs(pid20) == 0

***** Ideology *****
gen ideo16 = V161126 - 4 if V161126 > 0
replace ideo16 = 0 if V161126 == 99
gen ideo20 = V201200 - 4 if V201200 > 0
replace ideo20 = 0 if V201200 == 99

gen ideostrength16 = 3 if abs(ideo16) == 3
replace ideostrength16 = 2 if abs(ideo16) == 2
replace ideostrength16 = 1 if abs(ideo16) == 1
replace ideostrength16 = 0 if abs(ideo16) == 0

gen ideostrength20 = 3 if abs(ideo20) == 3
replace ideostrength20 = 2 if abs(ideo20) == 2
replace ideostrength20 = 1 if abs(ideo20) == 1
replace ideostrength20 = 0 if abs(ideo20) == 0

***** PID-IDEO Sorting *****
replace ideostrength16 = ideostrength16 + 1
replace pidstrength16 = pidstrength16 + 1
gen sorting16 = abs(pid16 - (-1 * ideo16)) * ideostrength16 * pidstrength16

replace ideostrength20 = ideostrength20 + 1
replace pidstrength20 = pidstrength20 + 1
gen sorting20 = abs(pid20 - (-1 * ideo20)) * ideostrength20 * pidstrength20

***** Demographics *****
gen edu16 = .
replace edu16 = 1 if V161270 == 1 | V161270 == 2 | V161270 == 3 | V161270 == 4 
replace edu16 = 2 if V161270 == 5 | V161270 == 6 | V161270 == 7 | V161270 == 8 
replace edu16 = 3 if V161270 == 9
replace edu16 = 4 if V161270 == 90
replace edu16 = 5 if V161270 == 10 | V161270 == 11 | V161270 == 12 
replace edu16 = 6 if V161270 == 13 
replace edu16 = 7 if V161270 == 14 | V161270 == 15 | V161270 == 16 
label define edulab 1 "8 grades or less" 2 "9-12 grades" 3 "High school" ///
	4 "HS + non-academic training" 5 "Some college" 6 "BA" 7 "Advanced"
label values edu edulab

gen income16 = V161361x if V161361x > 0 
replace income16 = V162309x if V162309x > 0 

gen race16 = V161310x if V161310x > 0 
gen white16 = 0
replace white16 = 1 if race16 == 1
gen black16 = 0
replace black16 = 1 if race16 == 2

gen female16 = 0
replace female16 = 1 if V161342 == 2

gen age16 = V161267 if V161267 > 0 
replace age16 = (V201507x-4) if age16 == . & V201507x > 0

gen south16 = .
replace south16 = 0 
replace south16 = 1 if V163003 == 3
label define southern 0 "0 Non-South" 1 "1 South"
label values south southern

gen church16 = .
replace church16 = 0 if V161241 == 2
replace church16 = 1 if V161242 == 1
replace church16 = 2 if V161242 == 2
replace church16 = 3 if V161242 == 3
replace church16 = church16/3

***** Political Interest *****
gen interest16 = 4-V161004 if V161004 > 0 
label define interestlab 1 "Not much interested" ///
	2 "Somewhat interested" 3 "Very much interested"
label values interest interestlab

***** Party Feeling Thermometers *****
gen reptherm16 = V161096 if V161096 >= 0 & V161096 <= 100
gen demtherm16 = V161095 if V161095 >= 0 & V161095 <= 100
gen partydifftherm16 = abs(demtherm16 - reptherm16)

gen reptherm20 = V201157 if V201157 >= 0
gen demtherm20 = V201156 if V201156 >= 0
gen partydifftherm20 = abs(demtherm20 - reptherm20)

***** Candidate Feeling Thermometers *****
gen rcandtherm16 = V162079 if V162079 >= 0 & V162079 <= 100
gen dcandtherm16 = V162078 if V162078 >= 0 & V162078 <= 100
gen diffcandtherm16 = abs(dcandtherm16 - rcandtherm16)

gen rcandtherm20 = V201152 if V201152 >= 0 
gen dcandtherm20 = V201151 if V201151 >= 0
gen diffcandtherm20 = abs(dcandtherm20 - rcandtherm20)

***** Ideological Group Feeling Thermometers *****
gen contherm16 = V162101 if V162101 >= 0 & V162101 <= 100
gen libtherm16 = V162097 if V162097 >= 0 & V162097 <= 100
gen diffideotherm16 = abs(libtherm16 - contherm16)

gen contherm20 = V202164 if V202164 >= 0
gen libtherm20 = V202161 if V202161 >= 0
gen diffideotherm20 = abs(libtherm20 - contherm20)


***** Issue Attitudes *****

* Enders and Lupton don't recode the "Haven't though much about it" (99) respondents to scale midpoint, but results hold if I do.

* Government spending and services
gen selfservice16 = 4-V161178 if V161178 > 0 & V161178 != 99
///replace selfservice16 = 0 if V161178 == 99
label define servicelab -3 "Government should provide many more services" ///
	3 "Government should provide many fewer services"
label values selfservice16 servicelab

gen selfservice20 = 4-V201246 if V201246 > 0 & V201246 != 99
///replace selfservice20 = 0 if V201246 == 99
label values selfservice20 servicelab

* Defense spending
gen selfdefense16 = V161181-4 if V161181 > 0 & V161181 != 99
///replace selfdefense16 = 0 if V161181 == 99
label define defenselab -3 "Greatly decrease defense spending" ///
	3 "Greatly increase defense spending"
label values selfdefense16 defenselab

gen selfdefense20 = V201249-4 if V201249 > 0 & V201249 != 99
///replace selfdefense20 = 0 if V201249 == 99
label values selfdefense20 defenselab

* Health insurance
gen selfinsure16 = V161184-4 if V161184 > 0 & V161184 != 99
///replace selfinsure16 = 0 if V161184 == 99
label define insurelab -3 "Government insurance plan" 3 "Private insurance plan"
label values selfinsure16 insurelab

gen selfinsure20 = V201252-4 if V201252 > 0 & V201252 != 99
///replace selfinsure20 = 0 if V201252 == 99
label values selfinsure20 insurelab

* Guarenteed jobs
gen selfjobs16 = V161189-4 if V161189 > 0 & V161189 != 99
///replace selfjobs16 = 0 if V161189 == 99
label define jobslab -3 "Government see to job and good standard of living" ///
	3 "Government let each person get ahead on his own"
label values selfjobs16 jobslab

gen selfjobs20 = V201255-4 if V201255 > 0 & V201255 != 99
///replace selfjobs20 = 0 if V201255 == 99
label values selfjobs20 jobslab

* Aid to blacks
gen selfaid16 = V161198-4 if V161198 > 0 & V161198 != 99
///replace selfaid16 = 0 if V161198 == 99
label define aidlab -3 "Government should help minority groups" ///
	3 "Minority groups should help themselves"
label values selfaid16 aidlab

gen selfaid20 = V201258-4 if V201258 > 0 & V201258 != 99
///replace selfaid20 = 0 if V201258 == 99
label values selfaid20 aidlab

gen issex1 = abs(selfdefense16 - 0)
gen issex2 = abs(selfservice16 - 0)	
gen issex3 = abs(selfaid16 - 0)	
gen issex4 = abs(selfinsure16 - 0)	
gen issex5 = abs(selfjobs16 - 0)	
alpha issex1-issex5, gen(issextreme16)
label var issextreme16 "Issue Extremity"

***** Political Values *****

* Egal 1
gen equalopp16 = V162243 - 1 if V162243 > 0 
gen equalopp20 = V202260 - 1 if V202260 > 0
label define equalopportunity 0 "0 Agree strongly" 1 "1 Agree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Disagree somewhat" ///
	4 "4 Disagree strongly"
label values equalopp16 equalopportunity
label values equalopp20 equalopportunity

* Egal 2
gen lessequal16 = V162244 if V162244 > 0 
gen lessequal20 = V202261 if V202261 > 0
recode lessequal16 (5=0) (4=1) (3=2) (2=3) (1=4)
recode lessequal20 (5=0) (4=1) (3=2) (2=3) (1=4)
label define lessequality 0 "0 Disagree strongly" 1 "1 Disagree somewhat" /// 
	2 "2 Neither agree nor disagree" 3 "3 Agree somewhat" 4 "4 Agree strongly"
label values lessequal16 lessequality
label values lessequal20 lessequality

* Egal 3
gen unequal16 = V162245 if V162245 > 0 
gen unequal20 = V202262 if V202262 > 0
recode unequal16 (5=0) (4=1) (3=2) (2=3) (1=4)
recode unequal20 (5=0) (4=1) (3=2) (2=3) (1=4)
label define unequalchance 0 "0 Disagree strongly" 1 "1 Disagree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Agree somewhat" 4 "4 Agree strongly"
label values unequal16 unequalchance
label values unequal20 unequalchance

* Egal 4
gen fewer16 = V162246 - 1 if V162246 > 0
gen fewer20 = V202263 - 1 if V202263 > 0
label define fewerproblems04 0 "0 Agree strongly" 1 "1 Agree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Disagree somewhat" ///
	4 "4 Disagree strongly"
label values fewer16 fewerproblems04
label values fewer20 fewerproblems04

* Moral Trad 1
gen changing16 = V162207 - 1 if V162207 > 0
gen changing20 = V202264 - 1 if V202264 > 0 
label define changingmorals 0 "Agree strongly" 1 "Agree somewhat" ///
	2 "Neither agree nor disagree" 3 "Disagree somewhat" 4 "Disagree strongly"
label values changing16 changingmorals
label values changing20 changingmorals

* Moral Trad 2
gen family16 = V162210 if V162210 > 0 
gen family20 = V202265 if V202265 > 0
recode family16 (5=0) (4=1) (3=2) (2=3) (1=4)
recode family20 (5=0) (4=1) (3=2) (2=3) (1=4)
label define familyties 0 "0 Disagree strongly" 1 "1 Disagree somewhat" /// 
	2 "2 Neither agree nor disagree"  3 "3 Agree somewhat" 4 "4 Agree strongly"
label values family16 familyties
label values family20 familyties

***** Value Extremity *****
alpha equalopp16 lessequal16 unequal16 fewer16 ///
	changing16 family16, detail item ///
	generate(valuescale16) 
alpha equalopp20 lessequal20 unequal20 fewer20 ///
	changing20 family20, detail item ///
	generate(valuescale20) 

gen valuepold16 = .
gen valuepolr16 = .	
gen valuepold20 = .
gen valuepolr20 = .	
	
sum valuescale16 if valuescale16 != . & rep16 == 1, meanonly
replace valuepold16 = valuescale16 - r(mean) if valuescale16 != . & rep16 == 0
sum valuescale16 if valuescale16 != . & rep16 == 0, meanonly
replace valuepolr16 = valuescale16 - r(mean) if valuescale16 != . & rep16 == 1

sum valuescale20 if valuescale20 != . & rep20 == 1, meanonly
replace valuepold20 = valuescale20 - r(mean) if valuescale20 != . & rep20 == 0
sum valuescale20 if valuescale20 != . & rep20 == 0, meanonly
replace valuepolr20 = valuescale20 - r(mean) if valuescale20 != . & rep20 == 1

foreach v of var valuepold16 - valuepolr20{ 
	replace `v' = abs(`v')
}

egen valuepol16 = rowtotal(valuepold16 valuepolr16)
egen valuepol20 = rowtotal(valuepold20 valuepolr20)

***** Subset to Necessary Variables and Save Coded Data *****
keep version V200001 V160001_orig V161270 V161361x V161267 V200011b _merge-valuepol20
save "anes1620panel_coded.dta", replace


***** Descriptive Statistics (Table 1) *****
	foreach var in valuepol16 partydifftherm16 diffideotherm16 diffcandtherm16 sorting16 issextreme16 interest16 edu16 age16 income16 church16 female16 white16 black16 south16 {
		qui sum `var'
		replace `var' = (`var' - `r(min)') / (`r(max)'-`r(min)')

	} // note I don't actually want these recodes for anything other than the values listed in Table 1, so I do these after saving the coded data file. 
	
sum valuepol16 partydifftherm16 diffideotherm16 diffcandtherm16 sorting16 issextreme16 interest16 church16 female16 white16 black16 south16 [aweight=weight], det
svy: tab V161270 if V161270 > 0 & V161270 < 90 // cats 13-16 add up to just over 32% with a BA or higher
tabstat V161361x [aweight=weight] if V161361x > 0, stats(n median) // cat 17 corresponds to $60,000-$64,999
tabstat V161267 [aweight=weight] if V161267 > 0, stats(n median) 

